package com.gzdh.dishrecognition.ui.pre;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.iot.bpaas.api.BPaaSApi;
import com.alipay.iot.bpaas.api.service.BPaaSCallback;
import com.alipay.iot.bpaas.api.service.BPaaSResponse;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.ProductConst;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.config.SystemConfig;
import com.gzdh.dishrecognition.emu.DeviceTypeEmus;
import com.gzdh.dishrecognition.model.Good;
import com.gzdh.dishrecognition.model.ProductCartModel;
import com.gzdh.dishrecognition.model.Trade;
import com.gzdh.dishrecognition.ui.adapter.defaultview.DefaultCartPreAdapter;
import com.gzdh.dishrecognition.ui.base.BasePresentation;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ProductPre extends BasePresentation {

    private RecyclerView productRv;

    private Context mContext;

    private DefaultCartPreAdapter defaultCartAdapter;

    public ProductPre(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.pre_product;
    }

    @Override
    protected void initView() {
        mContext = this.getContext();
        productRv = findViewById(R.id.productRv);
    }

    @Override
    protected void initData() {
        defaultCartAdapter = new DefaultCartPreAdapter(mContext);
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(mContext);
        productRv.setLayoutManager(layoutManager2);
        productRv.setAdapter(defaultCartAdapter);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void showData() {
        defaultCartAdapter.notifyDataSetChanged();
        if (SystemConfig.getInstance().getDeviceType() == DeviceTypeEmus.ali || SystemConfig.getInstance().getDeviceType() == DeviceTypeEmus.jeBaoAli) {
            List<ProductCartModel> productCartModelList = ProductConst.getInstance().productCartModelList;
            if (productCartModelList.size() > 0) {
                Bundle bundle = new Bundle();
                bundle.putString("page", "sku");
                bundle.putString("templateId", SystemConfig.getInstance().getTemplateId());
                bundle.putString("operateType", "show");
                BigDecimal amountPrice = new BigDecimal(0);
                List<Good> list = new ArrayList<>();
                for (ProductCartModel goo : productCartModelList) {
                    Good commodity = new Good();
                    commodity.name = goo.getName();
                    commodity.number = String.valueOf(goo.getNumber());
                    commodity.actualPrice = goo.getPrice();
                    BigDecimal price = new BigDecimal(goo.getNumber()).multiply(new BigDecimal(goo.getPrice()));
                    amountPrice = amountPrice.add(price);
                    commodity.finActualPrice = price.toPlainString();
                    list.add(commodity);
                }
                Trade trade = new Trade();
                trade.actualAmount = "￥" +String.format(amountPrice.toPlainString(), "￥%1$.2f", 21F);
                trade.totalAmount = "￥" + String.format(amountPrice.toPlainString(), "￥%1$.2f", 24F);
                trade.totalDiscount = String.format("0", "￥%1$.2f", 3F);


                JSONObject jsonObject = new JSONObject();
                jsonObject.put("goods", JSON.toJSON(list));
                jsonObject.put("trade", JSON.toJSON(trade));

                bundle.putString("templateParams", jsonObject.toString());

                BPaaSApi.getInstance().startBPaaSService(SystemConfig.getInstance().getAbcpApplicationAppid(), "BPaaSTemplateChannel", bundle, new BPaaSCallback() {
                    @Override
                    public void onResponse(BPaaSResponse response) {
//                        responseInvoke(response, "showSku");
                    }

                    @Override
                    public void onEvent(String eventId, String data, Bundle bundle) {
//                        Log.i(TAG, "onEvent eventId:" + eventId + ",data:" + data);
                    }
                });
            }

        }


    }

    public void clear() {
        if (SystemConfig.getInstance().getDeviceType() == DeviceTypeEmus.ali || SystemConfig.getInstance().getDeviceType() == DeviceTypeEmus.jeBaoAli) {
            Bundle bundle = new Bundle();
            bundle.putString("page", "sku");
            bundle.putString("templateId", SystemConfig.getInstance().getTemplateId());
            bundle.putString("operateType", "close");

            BPaaSApi.getInstance().startBPaaSService(SystemConfig.getInstance().getAbcpApplicationAppid(), "BPaaSTemplateChannel", bundle, new BPaaSCallback() {
                @Override
                public void onResponse(BPaaSResponse response) {
//                        responseInvoke(response, "closeSku");
                }

                @Override
                public void onEvent(String eventId, String data, Bundle bundle) {
//                        Log.i(TAG, "onEvent eventId:" + eventId + ",data:" + data);
                }
            });
        }
    }

    @Override
    public void show() {
        if (!SysConstConfig.getInstance().isAllowPreDin()) {
            super.show();
        }
    }
}
